/**
 * 
 */
package com.gmail.avereshchaga.web.validate;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import org.springframework.beans.factory.annotation.Autowired;

import com.gmail.avereshchaga.db.entity.IDivision;
import com.gmail.avereshchaga.db.service.IDivisionService;

/**
 * @author <a href="mailto:avereshchaga@gmail.com">Vereshchaga Alexey</a>
 * 
 */
public class ExisitingDivisionValidator implements
        ConstraintValidator<ExisitingDivision, String> {

    @Autowired
    private IDivisionService divisionService;

    @Override
    public void initialize(ExisitingDivision constraintAnnotation) {
    }

    @Override
    public boolean isValid(String s,
            ConstraintValidatorContext constraintValidatorContext) {

        for (IDivision division : divisionService.findAll()) {
            String divisionName = division.getName();
            if (s.equalsIgnoreCase(divisionName)) {
                return false;
            }

        }
        return true;
    }
}
